<?php
namespace Wap\Controller;
use Think\Controller;
use Wap\Service\PraiseService;
use Wap\Service\CustomerService;

class WapController extends Controller{
	public $token;
	public $wecha_id;
	public $wxuser;
	public $user;
	public $siteUrl;
	public $praise; //点赞活动ID
	public $share = array( 'link' => '', 'desc' => '', 'title' => '', 'imgUrl' => '' );

	/**
	 *
	 *@TODO: get post 逻辑优化
	 */
	public function __construct(){
		parent::__construct();
		//$this->praise = I( "get.praise", "", "" );
		//$this->assign( "praise", $this->praise );//TODO: 优化
		//判断活动是否存在、开始、结束

		//$result = PraiseService::is_good( $this->praise, time() );
		//if( $result['good'] == false ){
			//$this->error( $result['msg'] );
		//}

		// $check = D("praise")->cache( true )
		// 	->field( array( 'token', 'title', 'picurl', 'info' ) )
		// 	->where( array( "id" => I( "get.praise", -1, "" ) ) )
		// 	->find();
		// $this->token = $check['token'];
		// $this->share['title'] = '诠释和平、放飞梦想，用心创作、为爱点赞。';
		// $this->share['imgUrl'] = C('SITE_URL').'/'.'Public/images/praise/sharepeaceSmall.jpg';
		// $this->share['desc'] = '2015·第四届全国少儿世界和平海报大赛喊你来拿千元大奖啦，还不快到碗里来！';

		// $this->assign('token',$this->token);//开发者平台token
		// session( "token", $this->token );

		// if($this->token&&!preg_match("/^[0-9a-zA-Z]{3,42}$/",$this->token)){//验证token格式
		// 	exit('error token');
		// }
		// $this->wxuser=S('wxuser_'.$this->token);//获取缓存内容
		// if (!$this->wxuser||1){//wxuser不能为空
		// 	$this->wxuser = D('wxuser')->where(array('token'=>$this->token))->find();//获取公众号信息
		// 	S('wxuser_'.$this->token,$this->wxuser);//写入缓存
		// }

		// $this->assign('wxuser',$this->wxuser);//写入模板
		// $this->siteUrl = C('SITE_URL');
		// if ($this->wxuser['wechat_type']==2&&!isset($_GET['code'])&&$this->wxuser['oauth']){
		// 	$customeUrl=$this->siteUrl.$_SERVER['REQUEST_URI'];
		// 	$this->share['link'] = $customeUrl;
		// 	$scope='snsapi_base';
		// 	$oauthUrl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->wxuser['appid'].'&redirect_uri='.urlencode($customeUrl).'&response_type=code&scope='.$scope.'&state=oauth#wechat_redirect';
		// 	header('Location: '.$oauthUrl);
		// 	exit();//修复逻辑错误
		// }else if(isset($_GET['code'])&&isset($_GET['state'])&&$_GET['state']=='oauth'){
		// 	$rt=$this->curlGet('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->wxuser['appid'].'&secret='.$this->wxuser['appsecret'].'&code='.$_GET['code'].'&grant_type=authorization_code');
		// 	$jsonrt=json_decode($rt,1);//静默授权返回值
		// 	$openid=$jsonrt['openid'];//获取openid
		// 	$access_token=$jsonrt['access_token'];//局部token
		// 	$_GET['wecha_id']=$openid;//构建openid
		// 	$this->wecha_id=$openid;//写入模板
		// 	if (!$openid){
		// 		$this->error('授权不对：'.$jsonrt['errcode'],'#');
		// 		exit();
		// 	}else{
		// 		//存储到SESSION中? 是使用缓存还是使用session
        //         CustomerService::setOpenID( $openid );
		// 		session( "wxuser.appid", $this->wxuser['appid'] );
		// 		session( "wxuser.appsecret", $this->wxuser['appsecret'] );
		// 		session( "wxuser.openid", $this->wecha_id );
		// 	}
		// }
		// //
		// $this->assign('wecha_id',$this->wecha_id);//写入模板
		// //
		// $this->user=S('user_'.$this->wxuser['uid']);
		// if (!$this->user||1){
		// 	$this->user=D('Users')->find(intval($this->wxuser['uid']));
		// 	S('user_'.$this->wxuser['uid'],$this->user);
		// }
		// $this->assign('user',$this->user);

	}

	function strExists($haystack, $needle)
	{
		return !(strpos($haystack, $needle) === FALSE);
	}
	function curlGet($url){
		$ch = curl_init();
		$header = "Accept-Charset: utf-8";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$temp = curl_exec($ch);
		return $temp;
	}
}
